package zclTool;

import java.io.IOException;
import java.util.List;
import java.util.ArrayList;


public class DelimitedGroupReader {

	/**
	 * @param args
	 */
	private DelimitedReader dr;
	int key;
	private String[] lastLine;

	public DelimitedGroupReader(String filename, int key) {
		try {
			this.dr = new DelimitedReader(filename);
			this.key = key;
			//readOneUsefulLine();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<String[]> read() throws IOException {
		String curKeyValue = "";
		if(lastLine == null){
			lastLine = dr.read();
		}
		if (lastLine != null) {
			ArrayList<String[]> result = new ArrayList<String[]>();
			if(lastLine.length<key) return result;
			curKeyValue = lastLine[key];
			do {
				String[] temp = new String[lastLine.length];
				System.arraycopy(lastLine, 0, temp, 0, lastLine.length);
				result.add(temp);
			} while ((lastLine = dr.read()) != null && lastLine[key].equals(curKeyValue));
			return result;
		} else {
			return null;
		}
	}
	public void close() throws Exception{
		this.dr.close();
	}
	public void readOneUsefulLine()throws Exception{
		do{
			lastLine = dr.read();
			if(lastLine == null){
				break;
			}
		}while(lastLine.length>key);
	}
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		DelimitedGroupReader dgr = new DelimitedGroupReader("instance4classify.sbtitle",1);
		List<String[]>lines;
		while((lines = dgr.read())!=null){
			System.out.println(lines.size());
		}
		dgr.close();
		
	}

}
